package com.wangzh.nio.zip;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipDemo {
    public static void main(String[] args) throws IOException {
        //读取zip的每一个zipentry对象，zipentry对象表示每一个文件夹或者文件，根据zip地址的相对路径
        //src
        File src = new File("D:\\learn\\test.zip");
        //dest
        File dest = new File("D:\\learn\\zip\\");
        unzip(src,dest);
    }

    private static void unzip(File src, File dest) throws IOException {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(src));
        ZipEntry entry;
        while ((entry=zis.getNextEntry()) != null){
            System.out.println(entry);
            if (entry.isDirectory()) {
                //文件夹  需要创建文件夹
                File file = new File(dest,entry.toString());
                file.mkdirs();
            }else {
                //文件 在当前目录下创建文件
                File file = new File(dest, entry.toString());
                FileOutputStream fos = new FileOutputStream(file);
                int b;
                while ((b=zis.read()) !=  -1){
                    fos.write(b);
                }
                fos.close();
                zis.closeEntry();
            }
        }
        zis.close();
    }
}
